package code.jzh.monitor.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import code.jzh.monitor.entity.methodinvoke.Anchor;
import code.jzh.monitor.entity.methodinvoke.MessageInvokeRecord;
import code.jzh.monitor.entity.methodinvoke.MethodChain;
import code.jzh.monitor.util.MethodMonitorUtil;




public class MonitorFilter implements Filter {
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		MethodMonitorUtil.remove();
		MethodMonitorUtil.init();
		chain.doFilter(request, response);
		printAnchors();
		printMethodChain();
	}
	
	private void printAnchors(){
		List<Anchor> list = MethodMonitorUtil.getAnchor(); 
		for(Anchor an : list){
			System.out.println(an);
		}
	}
	
	private void printMethodChain(){
		List<MessageInvokeRecord> list = MethodMonitorUtil.buildMethodInvokeList(MethodMonitorUtil.getAnchor());
		for(MessageInvokeRecord mir : list){
			System.out.println(mir);
		}
		MethodChain mc = MethodMonitorUtil.buildMethodChain(list);
		MethodMonitorUtil.printMethodChain(mc, 0);
//		MethodMessage mm = new MethodMessage(MethodMonitorUtil.getCallID(),mc); //建立MethodMessage(带callID)，先注掉
		
	}
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
}
	
